package cn.edu.sdjzu.xg.kcsj.security;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class RoleService {
    private RoleService(){}
    private static RoleDao roleDao = RoleDao.getInstance();
    private static RoleService roleService = new RoleService();


    public static RoleService getInstance(){
        return roleService;
    }

    // 查找全部用户
    public Collection<Role> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Role> roles = new TreeSet<>();
        try {
            roles = roleDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return roles;
    }

    // 按id查找用户
    public Role find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Role role = null;
        try {
            role = roleDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return role;
    }

    // 更新一条
    public boolean update(Role role) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = roleDao.update(role,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    // 增加
    public boolean add(Role role) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = roleDao.add(role,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = roleDao.delete(id,connection);
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}
